package com.ruoyi.web.controller.wx;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ByBs;
import com.ruoyi.system.domain.ByJzYhq;
import com.ruoyi.system.service.IByBsService;
import com.ruoyi.system.service.IByBsTimeService;
import com.ruoyi.system.service.IBySubjectClassifyService;
import com.ruoyi.system.service.impl.SaveImgService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/bs")
@Validated
public class WxBsController {

    @Autowired
    IByBsService bsService;

    @Autowired
    private IByBsTimeService bsTimeService;

    @Autowired
    private IBySubjectClassifyService subjectClassifyService;

    @Autowired
    private SaveImgService saveImgService;

    @PostMapping("isShowBsOrderTimeDetail")
    public AjaxResult isShowBsOrderTimeDetail(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        Object obj = bsService.isShowBsOrderTimeDetail(json);
        return AjaxResult.success(obj);
    }

    @PostMapping("isGetBsStatus")
    public AjaxResult isGetBsStatus(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
//        System.out.println("vvvsss:::::::"+json.toString());
        Object obj = bsService.isGetBsStatus(json);

        return AjaxResult.success(obj);
    }


    @PostMapping("getNextSubjectList")
    public AjaxResult getNextSubjectList(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        String id = json.getString("id");
        if(!StringUtils.isEmpty(id)){
            List<JSONObject> list = subjectClassifyService.getNextSubjectList(id);

            return AjaxResult.success(list);
        }

        return AjaxResult.success();
    }

    @PostMapping("getNowDateList")
    public AjaxResult getNowDateList(){
        Object obj = bsService.getNowDateList();
        return AjaxResult.success(obj);
    }

    @PostMapping("getBsDate")
    public AjaxResult getBsDate(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        if(!StringUtils.isEmpty(json.getString("id"))){
            List<JSONObject> list = bsTimeService.getBsDateMoreNow(json.getString("id"));
            return AjaxResult.success(list);
        }
        return AjaxResult.success();
    }

    @PostMapping("getBsByWxid")
    public AjaxResult getBsByWxid(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        JSONObject bsEntity = bsService.getBsByWxid(json);
        return AjaxResult.success(bsEntity);
    }

    @PostMapping("saveImg")
    public Object saveImg(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        System.out.println(json.toString());
        String baseStr = json.getString("baseStr");
        if(baseStr.contains("data:image/jpeg;")){//进行保存jpg文件
            String accUrl = saveImgService.saveJpg(baseStr);
            return AjaxResult.success(accUrl);
        }else if(baseStr.contains("data:image/png;")){// 进行保存png文件
            String accUrl = saveImgService.savePng(baseStr);
            return AjaxResult.success(accUrl);
        }

        return AjaxResult.success();
    }


    @PostMapping("manTime")
    public Object manTime(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        String time = json.getString("time");
        String status = json.getString("status");
        if(status.equals("0")){
            status = "1";
        }else if(status.equals("1")){
            status = "0";
        }

        String timeResult = "";

        JSONArray list = JSONArray.parse(json.getString("list"));
        List<JSONObject> nlist = new ArrayList<>();
        for(Object obj:list){
            JSONObject objJson = (JSONObject)obj;
            if(objJson.getString("time").equals(time)){
                objJson.remove("status");
                objJson.put("status",status);
            }
            nlist.add(objJson);
        }

        for(JSONObject objJSON:nlist){
            if(objJSON.getString("status").equals("1")){
                timeResult += objJSON.getString("time").toString()+";";
            }
        }


        Map<String,Object> map = new HashMap<>();
        map.put("list",nlist);
        map.put("timeResult",timeResult);


        return AjaxResult.success(map);
    }

    @PostMapping("manOnlyBsSel")
    public Object manOnlyBsSel(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        String id = json.getString("id");
//        String status = json.getString("status");

        JSONArray list = JSONArray.parse(json.getString("list"));
        List<JSONObject> nlist = new ArrayList<>();
        for(Object obj:list){
            JSONObject objJson = (JSONObject)obj;
            if(objJson.getString("id").equals(id)){
                objJson.remove("status");
                objJson.put("status","1");
            }else{
                objJson.remove("status");
                objJson.put("status","0");
            }
            nlist.add(objJson);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("list",nlist);
        map.put("result",id);

        return AjaxResult.success(map);
    }


    @PostMapping("manSelBsSchool")
    public Object manSelBsSchool(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        String time = json.getString("id");
        String status = json.getString("status");
        if(status.equals("0")){
            status = "1";
        }else if(status.equals("1")){
            status = "0";
        }

        List<JSONObject> rlist = new ArrayList<>();

        JSONArray list = JSONArray.parse(json.getString("list"));
        List<JSONObject> nlist = new ArrayList<>();
        for(Object obj:list){
            JSONObject objJson = (JSONObject)obj;
            if(objJson.getString("id").equals(time)){
                objJson.remove("status");
                objJson.put("status",status);
            }
            nlist.add(objJson);
        }

        for(JSONObject objJSON:nlist){
            if(objJSON.getString("status").equals("1")){
//                timeResult += objJSON.getString("id").toString()+";";
                rlist.add(objJSON);
            }
        }


        Map<String,Object> map = new HashMap<>();
        map.put("list",nlist);
        map.put("timeResult",rlist);


        return AjaxResult.success(map);
    }


    @PostMapping("manSubject")
    public Object manSubject(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        String subjectid = json.getString("subjectid");

        JSONArray list = JSONArray.parse(json.getString("list"));
        List<JSONObject> nlist = new ArrayList<>();
        for(Object obj:list){
            JSONObject objJson = (JSONObject)obj;
            if(objJson.getString("id").equals(subjectid)){
                objJson.remove("status");
                objJson.put("status","1");
            }else{
                objJson.remove("status");
                objJson.put("status","0");
            }
            nlist.add(objJson);
        }
        return AjaxResult.success(nlist);
    }


    @PostMapping("getBsSubject")
    public Object getBsSubject(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        List<JSONObject> list = bsService.getBsSubject(json);

        return AjaxResult.success(list);
    }


    @PostMapping("bsOrderTime")
    public Object bsOrderTime(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        Map<String, Object> map = bsService.bsOrderTime(json);

        return AjaxResult.success(map);
    }

    @PostMapping("pageBangshouIndexShow")
    public Object pageBangshouIndexShow(){

        List<ByBs> list = bsService.pageBangshouIndexShow();

        return AjaxResult.success(list);
    }

    @PostMapping("bsDetail")
    public Object bsDetail(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        String id = json.getString("id");
        JSONObject entity = bsService.bsDetailById(id);

        return AjaxResult.success(entity);
    }

    @PostMapping("pageBangshou")
    public Object pageBangshou(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);

        List<ByBs> list = bsService.pageBangshou(json);

        return AjaxResult.success(list);
    }


    @PostMapping("queryCount")
    public Object queryCount(@RequestBody String param){
        // System.out.println(param);
        JSONObject json = JSONObject.parseObject(param);
        String wxid = json.getString("wxid");
        JSONObject result = bsService.queryByWxid(wxid);

        return AjaxResult.success(result);
    }

    @PostMapping("bcbs")
    public Object bcbs(@RequestBody ByBs entity){
         if (entity.getSchoolId() == 0){
             entity.setSchoolId((entity.getSchool_id()));
         }
        JSONObject json = bsService.managSave(entity);
        return AjaxResult.success(json);
    }

    /***
     *获取家长VIP信息
     * @param param
     * @return
     */
    @PostMapping("getVip")
    public AjaxResult getVip(@RequestBody String param) {
        JSONObject json = JSONObject.parseObject(param);
        ByJzYhq yhq = bsService.getVip(json);
        String data = "";
        if (yhq != null) {
            data = yhq.getQname() + "  有效期至：" + yhq.getEndtime();
        }
        JSONObject vip = new JSONObject();
        vip.put("vip", data);
        return AjaxResult.success(vip);
    }
}
